package com.xzit.proxy;

import com.xzit.aop.Salesperson;
import com.xzit.aop.Waiter;

import java.lang.reflect.Proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class WelcomeProxy implements InvocationHandler {
    private Object target;

    public WelcomeProxy(Object target) {
        this.target = target;
    }
    public static Object getProxy(Object target) {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new WelcomeProxy(target)
        );
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //增强逻辑
        if(method.getName().equals("chooseGoods")||method.getName().equals("order")){
            System.out.println("您好,欢迎光临!");
        }
        //执行目标对象的方法
        Object result = method.invoke(target, args);
        if (target instanceof Salesperson && method.getName().equals("pay")) {
            System.out.println("慢走，欢迎再来");
        } else if (target instanceof Waiter && method.getName().equals("pay")) {
            System.out.println("请带好您的随身物品，慢走，欢迎再来");
        }
        return result;
    }
}
